package com.czmh.etc.onlinepubsys.configuration;

import com.czmh.etc.common.core.page.PageBean;
import com.czmh.etc.onlinepubsys.feign.newrun.YueClient;
import com.czmh.etc.onlinepubsys.json.JsonResult;
import com.czmh.etc.onlinepubsys.maper.EtcApplyMapper;
import com.czmh.etc.onlinepubsys.model.EtcApply;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
/*
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务*/
public class DynamicScheduleTask implements SchedulingConfigurer {
    @Mapper
    public interface CronMapper {
        @Select("select cron from task_cron where task_id=1")
        public String getCron();
    }

    @Autowired
    private CronMapper cronMapper;
    @Autowired
    private EtcApplyMapper etcApplyMapper;
    @Autowired
    private YueClient yueClient;

    /**
     * 执行定时任务.
     */
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.addTriggerTask(
                //1.添加任务内容(Runnable)
                () -> dealEtcApplayApprovalStatus(),
                //2.设置执行周期(Trigger)
                triggerContext -> {
                    //2.1 从数据库获取执行周期
                    String cron = cronMapper.getCron();
                    //2.2 合法性校验.
                    if (StringUtils.isEmpty(cron)) {
                       return null;
                    }
                    //2.3 返回执行周期(Date)
                    return new CronTrigger(cron).nextExecutionTime(triggerContext);
                }
        );
    }

    /**
     * 处理申请信息的申请状态
     */
    public void dealEtcApplayApprovalStatus() {
        log.info("========执行更新ETC申请信息状态========");
        int page = 1;
        int size = 100;
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("apply_status", 10);
        params.put("orderField", "createtime");
        params.put("orderSeq", "ASC");
        PageBean pageBean = new PageBean(page, size);
        params.put("offset", pageBean.getFirst());
        params.put("pageSize", pageBean.getPageSize());
        List<EtcApply> list = etcApplyMapper.getAll(params);
        while (!list.isEmpty()) {
            for (EtcApply etcApply : list) {
                JsonResult jsonResult = yueClient.checkOrder(etcApply.getNiurunApplyId());
                if (JsonResult.CODE_SUCCESS.equals(jsonResult.getCode())) {
                    Map<String, Object> data = (HashMap<String, Object>) jsonResult.getData();
                    Object object = data.get("status");
                }
            }
            page = page + 1;
            pageBean = new PageBean(page, size);
            params.put("offset", pageBean.getFirst());
            params.put("pageSize", pageBean.getPageSize());
            list = etcApplyMapper.getAll(params);
        }
    }
}